package com.xx.sg.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.sg.own.entity.LightingMonitoring;
import com.xx.sg.own.service.LightingMonitoringService;
import com.xx.sg.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/lightingMonitoring")
@Tag(name = "雷电监测API", description = "雷电监测API")
public class LightingMonitoringController {

    @Autowired
    private LightingMonitoringService lightingMonitoringService;

    @PostMapping("/page")
    @Operation(summary = "获取雷电监测列表", description = "获取雷电监测列表")
    public R page(@RequestParam(defaultValue = "1") Integer current,
                  @RequestParam(defaultValue = "10") Integer size,
                  @ModelAttribute LightingMonitoring lightingMonitoring) {

        LambdaQueryWrapper<LightingMonitoring> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(lightingMonitoring.getForestFarm()), LightingMonitoring::getForestFarm, lightingMonitoring.getForestFarm())
                .eq(StringUtils.isNotBlank(lightingMonitoring.getLevel()), LightingMonitoring::getLevel, lightingMonitoring.getLevel())
                .eq(StringUtils.isNotBlank(lightingMonitoring.getCurrentIntensity()), LightingMonitoring::getCurrentIntensity, lightingMonitoring.getCurrentIntensity())
                .eq(StringUtils.isNotBlank(lightingMonitoring.getForestryBureau()), LightingMonitoring::getForestryBureau, lightingMonitoring.getForestryBureau())
                .apply(StringUtils.isNotBlank(lightingMonitoring.getDate()), "to_char(occurrence_time, 'YYYY-MM-dd') = {0}", lightingMonitoring.getDate())
                .orderByDesc(LightingMonitoring::getOccurrenceTime);
        Page<LightingMonitoring> page = new Page<>(current, size);
        lightingMonitoringService.page(page, wrapper);
        R r = R.success(page);
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }


    @GetMapping("/info/{id}")
    @Operation(summary = "雷电监测详情", description = "雷电监测详情")
    public R info(@PathVariable("id") String id) {
        LightingMonitoring lightingMonitoring = lightingMonitoringService.getById(id);
        R r = R.success(lightingMonitoring);
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @PostMapping("/add")
    @Operation(summary = "添加雷电监测", description = "添加雷电监测")
    public R add(LightingMonitoring lightingMonitoring) {
        lightingMonitoringService.save(lightingMonitoring);
        R r = R.success();
        r.put("data", "成功");
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @PostMapping("/modify")
    @Operation(summary = "修改雷电监测", description = "修改雷电监测")
    public R modify(LightingMonitoring lightingMonitoring) {
        lightingMonitoringService.updateById(lightingMonitoring);
        R r = R.success();
        r.put("data", "成功");
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @GetMapping("/del/{id}")
    @Operation(summary = "删除雷电监测", description = "删除雷电监测")
    public R del(@PathVariable String id) {
        lightingMonitoringService.removeById(id);
        R r = R.success();
        r.put("data", "成功");
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }
}
